/**
 *
 */
package com.sinodata.bsm.cicp.collector.oracle;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * @author biht
 * Oracle口令状态采集器
 *
 */
public class PassAndLockedEstateCollector extends AbstractJDBCCollector {
    Logger logger = Logger.getLogger(PassAndLockedEstateCollector.class.getName());

    /**
     *
     */
    public PassAndLockedEstateCollector() {
    }

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();

        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        String dbtype = this.getParamMap().get("DBType");
        String dbname = this.paramMap.get("DBName");
        String sqlString = this.getParamMap().get("SQL");
        String lockDate = null;
        String expiryDate = null;
        String locked = "UNLOCKED";
        String expirtied = "UNLOCKED";
        String days = "";
        String status = "";
        String profile = "";
        ResultSet rs;

        try {
            rs = this.getCachedResultSet(dbtype, ip, dbname, sqlString);
            while (rs.next()) {
                lockDate = rs.getString(1);
                expiryDate = rs.getString(2);
                if (rs.getBigDecimal(3) != null) {
                    days = rs.getBigDecimal(3).intValue() + "";
                }
                status = rs.getString(4);
                profile = rs.getString(5);

            }
            if (profile.indexOf("CMSZ_PROF") < 0 && profile.indexOf("USER_PROF") < 0) {
                if (status.indexOf("LOCK") > -1) {
                    locked = "LOCKED";
                }
                if (days != "" && !days.equals("")) {
                    int et = Integer.parseInt(days);
                    if (et < 0) {
                        expirtied = "LOCKED";
                    }
                }
            }
            propValues[0] = ResPropertyValue.createPropValue(locked, propMetas[0]);
            propValues[1] = ResPropertyValue.createPropValue(expirtied, propMetas[1]);

            if (lockDate != null) {
                propValues[2] = ResPropertyValue.createPropValue(lockDate, propMetas[2]);
            } else {
                propValues[2] = ResPropertyValue.createPropValue("", propMetas[2]);
            }
            if (expiryDate != null) {
                propValues[3] = ResPropertyValue.createPropValue(expiryDate, propMetas[3]);
            } else {
                propValues[3] = ResPropertyValue.createPropValue("", propMetas[3]);
            }

            if (days == "" || days.equals("")) {
                days = "";
            }

            propValues[4] = ResPropertyValue.createPropValue(days, propMetas[4]);
            propValues[5] = ResPropertyValue.createPropValue(status, propMetas[5]);
            propValues[6] = ResPropertyValue.createPropValue(profile, propMetas[6]);

            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
        } catch (Exception e) {
            logger.error(e);
            throw new CollectorException(e);
        }
        return values;
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "DBType", "DBName", "SQL" };
    }

    @Override
    public String[] getPropMeta() {
        return new String[] { "IsUserLock", "IsPasswordInvalid", "LOCKDATE", "EXPIRYDATE", "DAYS", "STATUS", "PROFILE" };
    }
}
